<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
	<meta name="author" content="wangyupeng">
	<title>单位卡发卡</title>
	<script language="javascript" src="../script/print.js" type="text/javascript"></script>
	<script language="javascript" src="../script/common.js" type="text/javascript"></script>
	<script language="javascript" src="../script/card.js" type="text/javascript"></script>
	<script type="text/javascript" language="JavaScript" src="../script/tree.js"></script>
	<!--动态js-->
	<script language="javascript" type="text/javascript">
	//页面加载方法
	var qt;	
	var companyUser;
	var resultData;				//子窗口可以从该数据中获取单位信息，以减少查询次数
	var cOrg;
	var isPrintView = false;
	function load(){
		Eui.showNavigation();
		setPages(["mainview","readcardview","createcompanydiv","createdeptdiv","detview"]);
		cOrg = Common.cOrg();
		initActiveX();
	}
	//刷新未发卡司机列表
	function refreshTable(){
		CardMgnt.drivers.refresh();
	}
	//读主卡，查找单位客户帐户
	function readMasterCard(){
		try{getCard();}catch(e){showError(e.description);return;}
		var asn = getAsn();
		companyform.asn.value = asn;
		DWR.call("mgntQueryService.findOkCompanyUser",asn,null,showCompanyUser)
	}
	
	//通过客户编号查找单位客户帐户
	function findCompanyUser(){
		if (!companyform.uniqueId.value){
			showWarning("请输入或选择一个客户编号");return;
		}
		DWR.call("mgntQueryService.findOkCompanyUser",null,companyform.uniqueId.value,showCompanyUser)
	}
	
	//选择客户编号编号
	function selCardUser(){
		CardMgnt.selCardUser(function(v){
			companyform.uniqueId.value = v.uniqueId;
		},{cardUserType:CardUserType.COMPANY})
	}
	
	//显示单位客户信息
	function showCompanyUser(data){
		resultData = data;
		companyUser = data.cardUser;
		if (!CardMgnt.checkCompanyLocation(companyUser)) return;
		if (companyUser.status != CardUserStatus.OK) {showError("单位帐户处于"+Common.getText(CardUserStatus.CATEGORY,companyUser.status)+"状态，不能执行此操作！");return;}
	
		switchPage("readcardview");
		Common.setValues(companyUserViewForm,CardMgnt.parseCardUser(data));
		
		var fee = CardMgnt.getFee(Constants.ACCOUNT_FEE_SELL_CARD,companyUser.cardType);
		var foregift = CardMgnt.getFee(Constants.ACCOUNT_FOREGIFT_CARD,companyUser.cardType);
		companyUserViewForm.feeAmount.value = Format.fenToYuan(fee);
		companyUserViewForm.foregiftAmount.value = Format.fenToYuan(foregift);
		var options = {}
		options.status = CardUserStatus.UNACTIVATED;
		options.operatRender = function(v,r){
			return "<a href=# onclick='doIssueCard("+r[0]+")'>售卡</a>"
		}
		CardMgnt.drivers.showList("driversdiv",companyUser.id,null,options)
	}
	//售卡-司机卡
	function doIssueCard(cardUserId){
		if (!showQuestion("是否出售司机卡？")) return;
		var data;
		var asn;
		try{
			getCard();
			asn = getAsn();
			data = DWR.syncCall("cardMgntService.issueOrgCard",asn,cardUserId);
		}catch(e){showError(e.description);return;}
		
		switchPage("detview");
		isPrintView = true;
		CardMgnt.showPrintView("detview","issue_org_card_bill",data.logId,"售卡凭证");
		try{
			WebPrint.print("billview");
		}catch(e){
			showError("打印单据出错:"+e.description)
		}
	
		while(true){
			try{
				//throw new Error("假装产生了售卡坏卡！");
				CardMgnt.syncCardData(asn);
				break;
			}catch(e){
				if (showQuestion("同步卡数据失败！是否重试？<br>错误："+e.description)){
					continue;
				}
				if (showQuestion("售卡产生坏卡，是否换卡？")){
					CardMgnt.badCard.gotoPage(asn,CardMgnt.badCard.DRIVER);
				}
				return;
			}
		}
		
		if (data.cardUser.usePin)
			CardMgnt.startCardPin(asn);
		showInformation("售卡成功！");
	}
	
	//创建单位
	function createCompany(){
		switchPage("createcompanydiv");
		CardMgnt.customer.show("companycustomerdiv",null,CardMgnt.COMPANYALL,{isCreate:true});
		CardMgnt.cardUser.show("companyuserdiv",null,CardMgnt.COMPANYALL);
	}
	
	//保存单位客户帐户
	function saveCompany(){
		if (!checkForm(CardMgnt.customer.formName)) return;
		if (!checkForm(CardMgnt.cardUser.formName)) return;
		
		if (!showQuestion("是否保存单位帐户？")) return;
		var cc = CardMgnt.customer.get();
		if (!cc.checkIdno){
			var c = DWR.syncCall("customerService.getCompanyCustomerForCardUser",cc.idno,cc.idnoType);
			if (c){
				if (!showQuestion("档案已经存在，是否在该档案下继续创建客户资料？")){onCancel();return;}
				cc.id = c.id
			}
		}
		var cu = CardMgnt.cardUser.get();
		DWR.call("customerService.saveCompanyCustomerAndUser",cc,cu,function(result){
			showInformation("单位客户保存成功");
			resultData = result;
			//TODO 打印单位建档凭证
			if (showQuestion("是否创建单位部门？")){
				createDept()
			}else
				showCompanyUser(resultData);
		});
	}
	function createDept(){
		switchPage("createdeptdiv");
		CardMgnt.dept.showData("dept_tree_view",resultData);
	}
	function editDeptFinish(){
		showCompanyUser(resultData);
	}
	//重置
	function onCancel(){
		if (isPrintView){
			switchPage("readcardview");
			refreshTable();
		}else{
			switchPage("mainview");
			companyUser = null;
			resultData = null;
			Common.setValues(companyUserViewForm,{});
			companyform.reset();
		}
		isPrintView = false;
	}
	</script>
</head>
<body onload="load()">
	<!--页面导航-->
	<div class="nav"><div id="nav">&nbsp;</div></div>
	<div id="mainview">
		<div class="form">
			<form id="companyform" name="companyform">
				<div class="formTitle"><div class="l">查询单位客户</div><div class="r">
					<div style="margin-top:4px;margin-right:20px;"><a href="#" onclick="createCompany()">新建单位客户</a></div>
				</div></div>
				<table cellSpacing=0 cellPadding=4 width="100%" border=0 class="label4">
					<tr>
						<td>
							<label>单位主卡</label> <input type="text" name="asn" readonly class="readonly">
							<button onclick="readMasterCard()">读主卡</button>
						</td>
						<td>
							<label>客户编号</label> <input type="text" name="uniqueId"><a class="orga" href="#" onclick="selCardUser()">&nbsp;&nbsp;</a>
						</td>
						<td>
							<button onclick="findCompanyUser()">查 询</button>
						</td>
					</tr>	
				</table>
			</form>
		</div>
	</div>
	<div id="readcardview" style="display:none;">
		<div class="form">
			<div class="formTitle">单位帐号信息</div>
			<form id="companyUserViewForm" name="companyUserViewForm">
				<input type="hidden" name="cardType">
				<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="col4 label4">
					<tr>
						<td><label>单位名称</label> <input name="name" type="text" readonly class="readonly"></td>
						<td><label>证件类型</label> <input name="idnoTypeText" type="text" readonly class="readonly"></td>
						<td><label>证&nbsp;件&nbsp;号</label> <input name="idno" type="text" readonly class="readonly"></td>
					</tr>
					<tr>
						<td><label>客户编号</label> <input name="uniqueId" type="text" readonly class="readonly"></td>
						<td><label>开户机构</label> <input name="buildFileStationName" type="text" readonly class="readonly"></td>
						<td><label>状&nbsp;&nbsp;&nbsp;&nbsp;态</label> <input name="statusText" type="text" readonly class="readonly"></td>
					</tr>
					<tr>
						<td><label>卡片类型</label> <input name="cardTypeName" type="text" readonly class="readonly"></td>
						<td><label>押 金</label> <input name="foregiftAmount" type="text" readonly class="readonly">元/张</td>
						<td><label>手续费</label> <input name="feeAmount" type="text" readonly class="readonly">元/张</td>
					</tr>
				</table>
			</form>
		</div>
			<div id='tabsdiv' align="left" class="width97"></div>
			<div id="driversdiv" style="margin-top:-10px;">
			</div>
		<p>
			<button onclick="onCancel()">返 回</button>
		</p>
	</div>
	<div id="createcompanydiv" style="display:none;">
		<div id="companycustomerdiv" class="form">
		</div>
		<div id="companyuserdiv" class="form">
		</div>
		<p>
			<button onclick="saveCompany()">保 存</button>
			<button onclick="onCancel()">取 消</button>
		</p>
	</div>
	
	<div id="createdeptdiv" style="display:none;">
		<div id="dept_tree_view">
		</div>
		<p>
			<button onclick="editDeptFinish()">部门维护完成</button>
		</p>
	</div>
	<!--凭证页面-->
	<div id="detview" style="display:none;">
	</div>
</body>
</html>